﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;



public class BuildManager : MonoBehaviour
{
    public GameObject[] templates;

    Coroutine runner = null;
    public void StartRun()
    {
        if (transform.childCount== 0)
        {
            runner = StartCoroutine(GenerateBuilds());
        }
    }

    public void EndRun()
    {
        StopCoroutine(runner);
    }

    public void Init()
    {
        int i = transform.childCount;

        for (int t = 0; t < i; t++)
        {
            Destroy(transform.GetChild(t).gameObject);
        }
    }


    IEnumerator GenerateBuilds()
    {
        while (transform.childCount < 4)
        {
            GenerateBuild();
            yield return new WaitForSeconds(Random.Range(1.5f, 3.5f));
        }
    }

    private void GenerateBuild()
    {
        int i = Random.Range(0, templates.Length);

        Instantiate(templates[i], this.transform, false);
    }

    private void Start()
    {
        templates =  Resources.LoadAll<GameObject>("Prefabs/Builds");
    }
}
